package com.mtcarpenter.lambda;

import org.junit.Test;

import java.io.PrintStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @Author: mtcarpenter
 * @describe 方法引用
 * @Date: 2019/10/6
 * @Version 1.0
 */
public class Lambda02Test {

    /**
     * 1 类名::静态方法名
     */
    @Test
    public void test01(){
        System.out.println("****************  JDK 1.7 比较函数   ********************");
        Comparator<Integer> comp1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };
        int compare = comp1.compare(10, 20);
        System.out.println(compare);

        System.out.println("****************  JDK 1.8 比较函数   ********************");
        System.out.println("****************    Lambda 表达式    ********************");
        // Lambda 表达式
        Comparator<Integer> comp2 =  (o1, o2) ->Integer.compare(o1,o2);
        int comparator1 = comp2.compare(10,20);
        System.out.println(comparator1);

        System.out.println("****************    Lambda 方法引用  ********************");
        //  Lambda 方法引用
        Comparator<Integer>  comp3  =  Integer::compare;
        int compare1 = comp3.compare(10, 20);
        System.out.println(compare1);
    }


    /**
     * 2 对象::实例方法名
     */
    @Test
    public void test02(){
        Consumer<String> consumer = s -> System.out.println(s);
        consumer.accept("lambda 表达式");
        System.out.println("**************");
        // 方法引用 println 方法替换了 accept 方法
        PrintStream ps =  System.out;
        Consumer<String> consumer2 = ps::println;
        consumer2.accept("method reference");
    }

    /**
     * 3 类名::实例方法名
     */
    @Test
    public void test03(){
        Comparator<String> com1  = (o1,o2) -> o1.compareTo(o2);
        System.out.println(com1.compare("a","b"));

        System.out.println("**************");

        Comparator<String> com2 = String::compareTo;
        System.out.println(com2.compare("a","c"));
    }


    /**
     *4 类名::new
     */
    @Test
    public void test04() {
        System.out.println("-------------------- 原始方法 ----------------------");
        BiConsumer<Integer, Integer> sum1 = new BiConsumer<Integer, Integer>() {
            @Override
            public void accept(Integer integer, Integer integer2) {
                new MathOperations(integer,integer2);
            }
        };
        sum1.accept(10,20);

        System.out.println("-------------------- lambda 表达式 ----------------------");
        BiConsumer<Integer, Integer> addition1 = (a, b) -> new MathOperations(a, b);
        addition1.accept(10, 20);

        System.out.println("--------------------- 方法引用     ---------------------");
        BiConsumer<Integer, Integer> addition2 = MathOperations::new;
        addition2.accept(20, 30);
    }

    /**
     * 5 数组::new
     */
    @Test
    public void test06(){
        System.out.println("--------------------   原始方法  ---------------------");
        Function<Integer,String[]> function1 = new Function<Integer, String[]>() {
            @Override
            public String[] apply(Integer length) {
                return new String[length];
            }
        };
        String[] str1 = function1.apply(5);
        System.out.println(Arrays.toString(str1));

        System.out.println("-------------------- lambda 表达式 ----------------------");
        Function<Integer,String[]> function2 = length -> new String[length];
        String[] str2 = function2.apply(6);
        System.out.println(Arrays.toString(str2));

        System.out.println("---------------------- 方法引用  ----------------------");
        Function<Integer,String[]> function3 =  String[]::new;
        String[] str3 =function3.apply(10);
        System.out.println(Arrays.toString(str3));

    }

}
